﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Drawing;
using System.ComponentModel;

using XRpgLibrary.TileEngine;

namespace XLevelEditor.Controls
{
    class MapLayerView : DataGridView
    {
        protected new bool DesignMode
        {
            get
            {
                if (base.DesignMode)
                {
                    return true;
                }
                else
                {
                    Control parent = this.Parent;
                    while (parent != null)
                    {
                        ISite site = parent.Site;
                        if (site != null && site.DesignMode)
                            return true;
                        
                        parent = parent.Parent;
                    }

                    return false;
                }
            }
        }

        public int ActiveLayerIndex 
        {
            get
            {
                for(int i = 0, cnt = Rows.Count; i < cnt; i++)
                    if (Rows[i].Selected) 
                        return i;

                return -1;
            }
        }

        public MapLayerView()
            : base()
        {
            this.AllowUserToResizeRows = false;
            this.SelectionMode = DataGridViewSelectionMode.FullRowSelect;
            this.AllowUserToOrderColumns = false;

            AutoGenerateColumns = false;
            RowHeadersVisible = true;
            ColumnHeadersVisible = false;

            if (!DesignMode)
            {
                DataGridViewCheckBoxColumn visibleColumn = new DataGridViewCheckBoxColumn();
                visibleColumn.DataPropertyName = "Visible";
                visibleColumn.HeaderText = "Visible";
                visibleColumn.Width = 40;

                DataGridViewTextBoxColumn nameColumn = new DataGridViewTextBoxColumn();
                nameColumn.DataPropertyName = "Name";
                nameColumn.HeaderText = "Layer Name";
                nameColumn.AutoSizeMode = DataGridViewAutoSizeColumnMode.Fill;

                Columns.Add(visibleColumn);
                Columns.Add(nameColumn);
            }
        }

        public void SelectFirstRow()
        {
            SelectRow(0);
        }

        public void SelectRow(int index)
        {
            foreach (DataGridViewRow row in Rows)
                row.Selected = false;

            if (Rows.Count > 0 && index < Rows.Count)
                Rows[index].Selected = true;
        }

        #region Events

        public override void NotifyCurrentCellDirty(bool dirty)
        {
            if (this.IsCurrentCellDirty && this.CurrentCell is DataGridViewCheckBoxCell)
            {
                this.CommitEdit(DataGridViewDataErrorContexts.Commit);
                this.EndEdit();
            }

            base.NotifyCurrentCellDirty(dirty);
        } 
        #endregion
    }
}
